We’re going to make some plotly plots.
library(tidyverse)
library(p8105.datasets)
library(plotly)
Lets get data.
data("nyc_airbnb")
nyc_airbnb %>%
mutate(rating = review_scores_location / 2) %>%
select(
borough = neighbourhood_group, neighbourhood, price, room_type, lat, long, rating) %>%
filter(!is.na(rating),
borough == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500)
## # A tibble: 7,568 × 7
## borough neighbourhood price room_type lat long rating
## <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl>
## 1 Manhattan Battery Park City 165 Entire home/apt -74.0 40.7 4.5
## 2 Manhattan Battery Park City 225 Entire home/apt -74.0 40.7 5
## 3 Manhattan Battery Park City 299 Entire home/apt -74.0 40.7 5
## 4 Manhattan Battery Park City 168 Entire home/apt -74.0 40.7 5
## 5 Manhattan Battery Park City 100 Entire home/apt -74.0 40.7 5
## 6 Manhattan Battery Park City 225 Entire home/apt -74.0 40.7 5
## 7 Manhattan Battery Park City 250 Entire home/apt -74.0 40.7 5
## 8 Manhattan Battery Park City 110 Entire home/apt -74.0 40.7 4.5
## 9 Manhattan Battery Park City 249 Entire home/apt -74.0 40.7 5
## 10 Manhattan Battery Park City 325 Entire home/apt -74.0 40.7 5
## # … with 7,558 more rows
Lets make a scatterplot!!
nyc_airbnb %>%
mutate(
text_label = str_c("Price: $", price)
) %>%
plot_ly(
x = ~lat, y = ~long, color = ~price,
type = "scatter", mode = "markers",
alpha = 0.5, text = ~text_label
)
can we make boxplots
nyc_airbnb %>%
mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>%
plot_ly(y = ~price, color = ~neighbourhood, type = "box", colors = "viridis")
can we make a bar plot
##first you need to define the number of observations in each to make the height of the bar
nyc_airbnb %>%
count(neighbourhood) %>%
mutate(neighbourhood = fct_reorder(neighbourhood, n)) %>%
plot_ly(x = ~neighbourhood, y = ~n, color = ~neighbourhood, type = "bar", colors = "viridis")
##ggplotly.. MUCH CH
ggp_catterplot =
nyc_airbnb %>%
ggplot(aes(x = lat, y = long, color = price)) +
geom_point(alpha = 0.25)
ggplotly(ggp_catterplot)